using UnityEngine;

namespace HEFramework
{
    /// <summary>
    ///
    /// 音源基类
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-12-19 15:59:29
    /// ----------------------------------------
    /// </summary>
    public class AudioSourceBase : IAudioSource
    {
        /// <summary>
        /// 音源
        /// </summary>
        protected AudioSource audioSource;

        /// <summary>
        /// 基础值（对内）
        /// </summary>
        protected float in_volum;

        /// <summary>
        /// 基础值（对外）
        /// </summary>
        protected float out_volum;

        /// <summary>
        /// 是否使用中
        /// </summary>
        protected bool isUse;


        /// <summary>
        /// 是否正在过渡中
        /// </summary>
        private bool isTransition;

        /// <summary>
        /// 变化时间
        /// </summary>
        private float time;

        /// <summary>
        /// 到达值
        /// </summary>
        private float to_value;

        /// <summary>
        /// 每秒变化值
        /// </summary>
        private float vary;


        public bool IsUse => isUse;


        /// <summary>
        /// 初始化
        /// </summary>
        public void Awake(AudioSource _audioSource)
        {
            audioSource = _audioSource;
            in_volum = 1;
            out_volum = 1;
            isTransition = false;
            audioSource.playOnAwake = false;
            InAwake();
        }

        /// <summary>
        /// 初始化（重写）
        /// </summary>
        protected virtual void InAwake()
        {
        }


        /// <summary>
        /// 帧更新
        /// </summary>
        public void Update()
        {
            if (audioSource.isPlaying)
            {
                if (isTransition)
                {
                    //表示继续变化
                    if (time > 0)
                    {
                        time = time - Time.deltaTime;
                        in_volum = in_volum + vary * Time.deltaTime;
                    }
                    else
                    {
                        time = 0;
                        in_volum = to_value;
                        isTransition = false;
                    }
                }

                //计算公式 （内部基础值*对外基础值）
                audioSource.volume = in_volum * out_volum;
            }

            InUpdate();
        }

        /// <summary>
        /// 帧更新（重写）
        /// </summary>
        protected virtual void InUpdate()
        {
        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Dispose()
        {
            audioSource.Stop();
            isTransition = false;
            AudioSourcePlayer.Instance.RemoveAudioSource(this);
            GameObject.Destroy(audioSource.gameObject);
        }

        /// <summary>
        /// 结束（重写）
        /// </summary>
        protected virtual void InDispose()
        {
        }

        /// <summary>
        /// 回收
        /// </summary>
        public void Recycle()
        {
            audioSource.Stop();
            isTransition = false;
            isUse = false;
        }


        public void Play()
        {
            audioSource.Play();
        }

        public void Play(float _end, float _time)
        {
            Play();
            Transition(in_volum, _end, _time);
        }

        public void Play(float _start, float _end, float _time)
        {
            Play();
            Transition(_start, _end, _time);
        }

        public void Play(AudioClip _audioClip)
        {
            audioSource.clip = _audioClip;
            audioSource.Play();
        }

        public void Play(AudioClip _audioClip, float _end, float _time)
        {
            Play(_audioClip);
            Transition(in_volum, _end, _time);
        }

        public void Play(AudioClip _audioClip, float _start, float _end, float _time)
        {
            Play(_audioClip);
            Transition(_start, _end, _time);
        }


        public void PlayOneShot()
        {
            audioSource.PlayOneShot(audioSource.clip);
        }

        public void PlayOneShot(float _end, float _time)
        {
            PlayOneShot();
            Transition(in_volum, _end, _time);
        }

        public void PlayOneShot(float _start, float _end, float _time)
        {
            PlayOneShot();
            Transition(_start, _end, _time);
        }

        public void PlayOneShot(AudioClip _audioClip)
        {
            audioSource.clip = _audioClip;
            audioSource.PlayOneShot(audioSource.clip);
        }

        public void PlayOneShot(AudioClip _audioClip, float _end, float _time)
        {
            PlayOneShot(_audioClip);
            Transition(in_volum, _end, _time);
        }

        public void PlayOneShot(AudioClip _audioClip, float _start, float _end, float _time)
        {
            PlayOneShot(_audioClip);
            Transition(_start, _end, _time);
        }


        public void Stop()
        {
            audioSource.Stop();
        }

        public void Pause()
        {
            audioSource.Pause();
        }

        public void Mute(bool _isMute)
        {
            audioSource.mute = _isMute;
        }

        public void Loop(bool _isLoop)
        {
            audioSource.loop = _isLoop;
        }

        public void Volume(float _value)
        {
            if (_value < 0)
            {
                _value = 0;
            }

            if (_value > 1)
            {
                _value = 1;
            }

            out_volum = _value;
        }

        public void Use(bool _value)
        {
            isUse = _value;
        }


        /// <summary>
        /// 渐变
        /// </summary>
        /// <param name="_start">开始渐变的值</param>
        /// <param name="_end">结束渐变的值</param>
        /// <param name="_time">时间(s)</param>
        /// <returns></returns>
        private void Transition(float _start, float _end, float _time)
        {
            if (_time <= 0)
            {
                return;
            }

            if (_start == _end)
            {
                return;
            }

            if (_start < 0)
            {
                _start = 0;
            }

            if (_start > 1)
            {
                _start = 1;
            }

            if (_end < 0)
            {
                _end = 0;
            }

            if (_end > 1)
            {
                _end = 1;
            }

            in_volum = _start;
            to_value = _end;
            time = _time;
            vary = (_end - _start) / _time;
            isTransition = true;
        }
    }
}